package com.example.petshop.controller;

import com.example.petshop.entity.Goods;
import com.example.petshop.entity.Shop;
import com.example.petshop.service.GoodsService;
import com.example.petshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/getall")
    public List<Goods> getAll(){
        return goodsService.getAll();
    }


    @GetMapping("/details/{goodsId}")
    public ResponseEntity<Goods> getDetails(@PathVariable Integer goodsId) {
        Optional<Goods> goods = goodsService.getById(goodsId);
        return goods.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }
    // 更新商品状态的端点
    @PostMapping("/buy/{goodsId}")
    public ResponseEntity<?> buyGoods(@PathVariable Integer goodsId,@RequestParam("state") Integer state) {
        System.out.println("GoodsId: "+goodsId+"state: "+state);
        try {
            // 调用服务层的方法来更新商品状态
            goodsService.updateState(goodsId, state);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    // 新增端点获取状态为1的商品数据
    @GetMapping("/state/1")
    public ResponseEntity<List<Goods>> getGoodsByState() {
        List<Goods> goods = goodsService.getByState(1);
        if (goods.isEmpty()) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(goods);
        }
    }
    // 新增端点获取状态为2的商品数据
    @GetMapping("/state/2")
    public ResponseEntity<List<Goods>> getGoodsByState2() {
        List<Goods> goods = goodsService.getByState(2);
        if (goods.isEmpty()) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(goods);
        }
    }
    // 新增端点获取状态为3的商品数据
    @GetMapping("/state/3")
    public ResponseEntity<List<Goods>> getGoodsByState3() {
        List<Goods> goods = goodsService.getByState(3);
        if (goods.isEmpty()) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok(goods);
        }
    }
    @PostMapping("/create")
    public ResponseEntity<Void> createGoods(@RequestBody Goods goods) {
        System.out.println(goods);
        // 调用服务层的方法来创建新的商店
        goodsService.createGoods(goods);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/delete/{goodsId}")
    public ResponseEntity<Void> deleteGoods(@PathVariable Integer goodsId) {
        // 调用服务层的方法来删除商店
        goodsService.deleteGoods(goodsId);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update")
    public ResponseEntity<Void> updateGoods(@RequestBody Goods goods) {
        // 调用服务层的方法来更新商店
        goodsService.updateGoods(goods);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getCategory")
    public ResponseEntity<List<Goods>> getCategory(@RequestParam String category) {
        List<Goods> goodsList = goodsService.getGoodsByCategory(category);

        if (goodsList.isEmpty()) {
            return ResponseEntity.notFound().build();
        }

        // 只返回图片和名字
        List<Goods> filteredGoods = goodsList.stream()
                .map(g -> new Goods(null, g.getGoodsName(), g.getImages(), null, null, null, null, null, null,null,null,null,null))
                .collect(Collectors.toList());

        return ResponseEntity.ok(filteredGoods);
    }

    @GetMapping("/statistics")
    public ResponseEntity<List<Goods>> getGoodsStatisticsByShopId(@RequestParam Integer shopId) {
        List<Goods> goodsList = goodsService.getGoodsByShopId(shopId);

        if (goodsList.isEmpty()) {
            return ResponseEntity.notFound().build();
        }

        return ResponseEntity.ok(goodsList);
    }
}
